<?php

/*
 * 商品测试
 * @author wyb
 */

namespace Api\Api\Stock;

class Stock extends \Api\Api\Base {

    var $goodsModel;

    function __construct() {
        parent::__construct();
        $this->goodsModel = D("Goods");
    }

    /**
     * 根据sku 返回库存
     * @param type $data
     * @return type
     */
    public function query($data) {
        $sku = $data['sku'];
        $skulist = explode(",", $sku);
        $goods = $this->goodsModel->where(array('sku' => array("in", $skulist)))->field("stock_num,sku")->select();
        if (!$goods) {
            $this->setError(self::SKU_ERROR, "SKU不存在");
            return false;
        }
        $datas = array();
        foreach ($goods as $key => $value) {
            $datas[$value['sku']] = $value['stock_num'];
        }
        return $datas;
    }

    /*     * \
     * 库存减
     */

    public function minus($data) {

        $this->goodsModel->startTrans();
        $data['data'] = json_decode($data['data'], 1);
        $skulist = array_keys($data['data']);
        $order_id = $data['order_id'];
        $orderlogModel = M("order_stock_log");
        $logs = $orderlogModel->where(array("order_id" => $order_id, "type" => 0))->find();
        if ($logs) {
            $this->setError(self::SKU_ERROR, "该订单已经减过库存了");
            $this->goodsModel->rollback();
            return false;
        }
        //插入库存记录
        $insertdata = array("order_id" => $order_id, "type" => 0, "create_time" => time(), "parm" => serialize($data));
        if (!$orderlogModel->add($insertdata)) {
            $this->setError(self::SKU_ERROR, "插入订单操作日志失败");
            $this->goodsModel->rollback();
            return false;
        }
        $goodslist = $this->goodsModel->where(array('sku' => array('in', $skulist)))->field("stock_num,sku")->lock()->select();


//         $goods = $this->goodsModel->where(array('sku' => $sku))->field("stock_num")->find();
        if (!$goodslist) {
            $this->setError(self::SKU_ERROR, "SKU不存在");
            $this->goodsModel->rollback();
            $this->addstockerrlog($order_id, array_merge($data, array("error_msg" => "SKU不存在")));
            return false;
        }
        foreach ($goodslist as $key => $goods) {
//            if ($goods['stock_num'] < $data['data'][$goods['sku']]) {
//                $this->setError(self::STOCK_NOT_ENOUGH, "-{$goods['sku']}->库存不足");
//                $this->goodsModel->rollback();
//                return false;
//            }

            $flag = $this->goodsModel->where(array('sku' => $goods['sku']))->setDec("stock_num", $data['data'][$goods['sku']]);
            if ($flag === FALSE) {
//            $this->setError(self::UPDATE_FAIL,"更新失败");
                $this->setError(self::UPDATE_FAIL, "更新失败");
                $this->goodsModel->rollback();
                $this->addstockerrlog($order_id, array_merge($data, array("error_msg" => "减更新失败")));
                return false;
            }
        }

        $this->goodsModel->commit();

        return true;
    }

    public function plus($data) {

        $this->goodsModel->startTrans();
        $data['data'] = json_decode($data['data'], 1);
        $skulist = array_keys($data['data']);
        $order_id = $data['order_id'];
        $orderlogModel = M("order_stock_log");
        $logs = $orderlogModel->where(array("order_id" => $order_id, "type" => 1))->find();
        if ($logs) {
            $msg = "该订单已经加过库存了";
            $this->setError(self::SKU_ERROR, $msg);
            $this->goodsModel->rollback();
            return false;
        }
        //插入库存记录
        $insertdata = array("order_id" => $order_id, "type" => 1, "create_time" => time(), "parm" => serialize($data));
        if (!$orderlogModel->add($insertdata)) {
            $this->setError(self::SKU_ERROR, "插入订单操作日志失败");
            $this->goodsModel->rollback();
            return false;
        }

        $goodslist = $this->goodsModel->where(array('sku' => array('in', $skulist)))->field("stock_num,sku")->lock()->select();

        if (!$goodslist) {
            $this->setError(self::SKU_ERROR, "SKU不存在");
            $this->goodsModel->rollback();
            $this->addstockerrlog($order_id, array_merge($data, array("error_msg" => "SKU不存在")));
            return false;
        }
        foreach ($goodslist as $key => $goods) {

            $flag = $this->goodsModel->where(array('sku' => $goods['sku']))->setInc("stock_num", $data['data'][$goods['sku']]);
            if ($flag === FALSE) {
//            $this->setError(self::UPDATE_FAIL,"更新失败");
                $this->setError(self::UPDATE_FAIL, "更新失败");
                $this->goodsModel->rollback();
                $this->addstockerrlog($order_id, array_merge($data, array("error_msg" => "jiakucun更新失败")));
                return false;
            }
        }

        $this->goodsModel->commit();

        return true;
    }

    function addstockerrlog($order_id, $data) {
        $orderlogModel = M("order_stock_log");
        $logs = $orderlogModel->where(array("order_id" => $order_id, "type" => 2))->find();
        if ($logs) {
            //update
            return $orderlogModel->where(array("order_id" => $order_id, "type" => 2))->save(array("parm" => serialize($data), 'create_time' => time()));
        } else {
            return $orderlogModel->add(array("parm" => serialize($data), 'create_time' => time(), "order_id" => $order_id, "type" => 2));
        }
    }

}
